package cn.kgc.controller;

import cn.kgc.common.annotation.AutoLog;
import cn.kgc.common.constant.LogType;
import cn.kgc.common.exceptionhandler.HttpException;
import cn.kgc.common.exceptionhandler.MyException;
import cn.kgc.common.util.PingYinUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.kgc.entity.Dict;
import cn.kgc.dto.DictDTO;
import cn.kgc.service.DictService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Locale;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import cn.kgc.vo.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.kgc.entity.Dict;

/**
 * (Dict)表控制层
 *
 * @author 石浩杰
 * @since 2022-09-24 08:30:37
 */
@RestController
@RequestMapping("dict")
@Api
@Validated
public class DictController {
    /**
     * 服务对象
     */
    @Resource
    private DictService dictService;

    /**
     * 分页查询所有数据
     *
     * @param current 分页对象
     * @return 所有数据
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询所有数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "name",value = "字典名字名字",paramType = "query",dataType = "int"),
    })
    @CrossOrigin
    @AutoLog(moduleName = "数据字典",title="数据字典",content = "分页查询数据字典",type = LogType.LOG_TYPE_OPERATOR)
    public IPage<Dict> selectAll(@RequestParam(defaultValue = "1") int current,
                                 @RequestParam(defaultValue = "5") int pageSize,
                                 @RequestParam(defaultValue = "") String name) {
        IPage<Dict> page=new Page<>();
        page.setCurrent(current);
        page.setSize(pageSize);
        LambdaQueryWrapper<Dict> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Dict::getName,name);
        lambdaQueryWrapper.orderByDesc(Dict::getName);
        dictService.page(page,lambdaQueryWrapper);
        return page;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @CrossOrigin
    @ApiOperation(value = "通过主键查询单条数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    @AutoLog(moduleName = "数据字典",title="数据字典",content = "查询数据字典",type = LogType.LOG_TYPE_OPERATOR)
    public Dict selectOne(@PathVariable Serializable id) {
        return this.dictService.getById(id);
    }

    /**
     * 新增数据
     *
     * @param dictDTO 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @CrossOrigin
    @AutoLog(moduleName = "数据字典",title="数据字典新增",content = "新增数据字典",type = LogType.LOG_TYPE_OPERATOR)
    public boolean insert(@RequestBody DictDTO dictDTO) {
        String name=dictDTO.getName();
        String code="";
        if (dictDTO.getType()==1){//如果字典类型是字典分类
            dictDTO.setPid(-1);//字典分类pid为-1
            code= PingYinUtil.getAllLeter(name).toUpperCase();//根据名称获得编码
        }else {
            int pid=dictDTO.getPid();
            Dict pDict=dictService.getById(pid);
            code=pDict.getCode()+"_"+PingYinUtil.getAllLeter(dictDTO.getName().toUpperCase());
        }
        Dict dict=new Dict();
        BeanUtils.copyProperties(dictDTO,dict);
        dict.setCode(code);
        return dictService.save(dict);
    }

    /**
     * 修改数据
     *
     * @param dictDTO 实体对象
     * @return 修改结果
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改信息", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @CrossOrigin
    @AutoLog(moduleName = "数据字典",title="数据字典",content = "更新数据字典",type = LogType.LOG_TYPE_OPERATOR)
    public boolean update(@RequestBody DictDTO dictDTO) {
        Dict dict = this.dictService.getById(dictDTO.getId());
        BeanUtils.copyProperties(dictDTO, dict);
        return this.dictService.updateById(dict);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据主键删除信息", produces = "application/json;charset=utf-8",
            httpMethod = "DELETE", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    @CrossOrigin
    @AutoLog(moduleName = "数据字典",title="数据字典",content = "删除数据字典",type = LogType.LOG_TYPE_OPERATOR)
    public boolean delete(@PathVariable Long id) {
        return this.dictService.removeById(id);
    }
}
